package com.ruoyi.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.logistics.config.hbase.HBaseConfig;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.logistics.domain.TraceLngLat;
import com.ruoyi.logistics.service.TraceLngLatService;
import com.ruoyi.logistics.service.TraceTransportService;
import com.ruoyi.logistics.utils.hbase.HBaseService;
import com.ruoyi.system.api.domain.TraceTransport;
import com.ruoyi.system.api.domain.vo.CropsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.image.ByteLookupTable;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("logistics")
@Slf4j
public class TraceTransportController {

    @Autowired
    TraceTransportService traceTransportService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    TraceLngLatService traceLngLatService;

    @Autowired
    private HBaseService hBaseService;

    private static final String GPS_PROPERTY = "gps_";


    //redis前缀 用于防止kafka重复消费
    private static final String PREVENT_RC = "traceTransport:";

    /**
     * 显示任务
     */
    @GetMapping("/listTransport/{driverId}")
    public AjaxResult list(@PathVariable("driverId") String driverId) {
        //获取信息
        List<TraceTransport> list = traceTransportService.listTransport(driverId);
        //返回
        return AjaxResult.success(list);
    }


    /**
     * 接受任务+hbase 保存经纬度
     * 派送完成
     */
    @PostMapping("/saveAndUpdateTransportInfoToDb")
    public AjaxResult saveAndUpdateTransportInfoToDb(@RequestBody TraceLngLat traceLngLat) {
        traceLngLatService.saveGpsTable(traceLngLat);
        return AjaxResult.success(traceTransportService.saveAndUpdateTransportInfoToDb(traceLngLat));
    }


    /**
     * 根据部门id获取农作物列表
     * 为系统提供入库远程调用的接口方法
     *
     * @param deptId
     * @return
     */
    @InnerAuth
    @GetMapping("/listCrops/{deptId}")
    public R<List<TraceTransport>> listCrops(@PathVariable("deptId") String deptId) {
        List<TraceTransport> list = traceTransportService.listCrops(deptId);
        return R.ok(list);
    }


    /**
     * 为系统提供入库远程调用的接口方法
     * 根据部门id获取农作物+物流列表
     *
     * @param deptId
     * @return
     */
    @InnerAuth
    @GetMapping("/queryCropsList/{deptId}")
    public R<List<CropsVo>> queryCropsList(@PathVariable("deptId") Integer deptId) {
        List<CropsVo> list = traceTransportService.queryCropsList(deptId);
        return R.ok(list);
    }


    /**
     * 是否出库
     *
     * @param cropsId
     * @return
     */
    @InnerAuth
    @GetMapping("/changeInToOut/{cropsId}")
    public R<Integer> changeInToOut(@PathVariable("cropsId") String cropsId) {
        int i = traceTransportService.changeInToOut(cropsId);
        return R.ok(i);
    }

    /**
     * 添加物流数据
     *
     * @param traceTransport
     * @return
     */
    @InnerAuth
    @PostMapping("/addTransport")
    public R<Integer> addTransport(@RequestBody TraceTransport traceTransport) {
        int i = traceTransportService.addTransport(traceTransport);
        return R.ok(i);
    }


    /**
     * 修改零售签收状态
     *
     * @param traceTransport
     * @return
     */
    @InnerAuth
    @PostMapping("/updateReceiveStatus")
    public R<Integer> updateReceiveStatus(@RequestBody TraceTransport traceTransport) {
        int i = traceTransportService.updateReceiveStatus(traceTransport);
        return R.ok(i);
    }


    /**
     * hbase 保存经纬度
     *
     * @param traceLngLat
     * @return
     */
    @PostMapping("saveGpsTable")
    public AjaxResult saveGpsTable(@RequestBody TraceLngLat traceLngLat){
        traceLngLatService.saveGpsTable(traceLngLat);
        return AjaxResult.success("保存成功");
    }

    /**
     * 通过 hbase 查询经纬度
     *
     * @param cropsId
     * @return
     */
    @GetMapping("/getGpsTable/{cropsId}")
    public AjaxResult getGpsTable(@PathVariable("cropsId") String cropsId){
        List<TraceLngLat> gpsTable = traceLngLatService.getGpsTable(cropsId);
        System.out.println(gpsTable);
        return AjaxResult.success(gpsTable);
    }

    /**
     * 删除对应的表
     *
     * @param cropsId
     * @return
     */
    @DeleteMapping("deleteTable")
    public AjaxResult deleteTable(String cropsId){
        traceLngLatService.deleteTable(cropsId);
        return AjaxResult.success("删除成功");
    }
}
